/**
 * 根据分隔符，拆解字符串
 * @param str 需要拆解的字符串
 */
export function split (str: string = '', delimiter = '|'): string[] {
  return str.split(delimiter)
}

function parseRuleItem (it = '', divider = "="): string {
  let [key, val] = it.split(divider)
  let value
  switch (key) {
    case 'required':
      value = true
      break
    case 'pattern':
      const eval2 = eval
      value = eval2(val)
      break
    default:
      value = `'${val}'`
  }
  return `${key}: ${value}`
}

export function formatRules (rules: string[] = [], delimiter = '|') {
  return rules.map(rule => {
    const ruleSet = split(rule, delimiter)
    return `{ ${ruleSet.map(rule => parseRuleItem(rule)).join(', ')} }`
  }).join(', ')
}
